﻿using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Transmute.Graphics
{
    public class AngleGradientBrush : GradientBrush
    {
        #region Properties

        private SpriteBatch spriteBatch;

        public float Precision = 0.006f;

        #endregion

        #region Constructor

        public AngleGradientBrush(int width, int height, Color startColor, Color endColor) : base(GraphicsHelper.GraphicsDevice, width, height, startColor, endColor, new Vector2(width / 2, height / 2), new Vector2(width, height))
        {
            spriteBatch = GraphicsHelper.SpriteBatch;
            Render();
        }

        public AngleGradientBrush(int width, int height, Vector2 centerPoint, Color startColor, Color endColor)
            : base(GraphicsHelper.GraphicsDevice, width, height, startColor, endColor, centerPoint, new Vector2((centerPoint.X != 0 ? 0 : width), (centerPoint.Y != 0 ? 0 : height)))
        {
            spriteBatch = GraphicsHelper.SpriteBatch;
            Render();
        }

        #endregion

        #region Methods

        public override void Render()
        {
            GraphicsDevice.SetRenderTarget(this);
            GraphicsDevice.Clear(Color.Transparent);

            var r = GraphicsHelper.GetDistance(StartPoint, EndPoint) * 5;
            Color color = StartColor;
            float count = 0f;
            var precision = 1f;
            
            spriteBatch.Begin();
                // Left side
                for (float y = 0; y < Height; y += precision)
                {
                    color = Color.Lerp(StartColor, EndColor, (float)((count / r)));
                    GraphicsHelper.DrawLine(StartPoint, new Vector2(-2, y), 3, color);
                    count += precision;
                }

                // Bottom side
                for (float x = 0; x < Width; x += precision)
                {
                    color = Color.Lerp(StartColor, EndColor, (float)((count / r)));
                    GraphicsHelper.DrawLine(StartPoint, new Vector2(x, Height + 2), 3, color);
                    count += precision;
                }

                // Right Side
                for (float y = Height; y > 0; y -= precision)
                {
                    color = Color.Lerp(StartColor, EndColor, (float)((count / r)));
                    GraphicsHelper.DrawLine(StartPoint, new Vector2(Width + 2, y), 3, color);
                    count += precision;
                }

                // Top Side
                for (float x = Width; x > 0; x -= precision)
                {
                    color = Color.Lerp(StartColor, EndColor, (float)((count / r)));
                    GraphicsHelper.DrawLine(StartPoint, new Vector2(x, -2), 3, color);
                    count += precision;
                }                
            spriteBatch.End();

            GraphicsDevice.SetRenderTarget(null);
        }

        #endregion
    }
}
